// pages/searchIndex/searchIndex.js
//搜索首页
let url_js = require("../../utils/url.js");
let common_js = require("../../utils/common.js");
let user_js = require("../../utils/user.js");
let search_js = require("../../utils/search.js");
let ubt_js = require("../../utils/ubt.js");
let name_js = require("../../utils/ubtButtonName.js");
let api = require("../../utils/apiRequest.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    contentStatus: -1, //1有数据
    adList: [],
    keywordList: [],
    searchHistoryList: [],
    keyword: '',
    placeholderInfo: {
      placeholder: '',
      index: 0,
    },
    timerObj: { //计时器对象
      timer: null,
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //业务埋点
    ubt_js.myPageView(common_js.currentPageName(), options);
    //1.禁止转发
    wx.hideShareMenu();
    //2.初始化数据
    this.initData(options);
    //3.网路请求获取数据
    this.getRemoteDatas();
  },

  //网路请求获取数据
  getRemoteDatas: function () {
    //1.广告
    //2.关键词
    Promise.all([api.wxRequest(url_js.searchAdvertUrl, {}), api.wxRequest(url_js.searchHotKeywordUrl, {})]).then(results => {
      this.handleSearchAdvertResponseData(results[0]);
      this.handleSearchHotKeywordResponseData(results[1]);
    }).catch(err => {
      //展示历史搜索
      let tmpPlaceholderInfo = this.data.placeholderInfo;
      tmpPlaceholderInfo.placeholder = "请输入搜索关键字";
      this.setData({
        keyPlaceHolder: tmpPlaceholderInfo,
        contentStatus: 1,
      });
      common_js.log(err);
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let tmpHistoryList = search_js.historyList();
    this.setData({
      searchHistoryList: tmpHistoryList
    });
  },

  //处理返回的广告
  handleSearchAdvertResponseData: function (res) {
    if (res.result.length > 0) {
      this.setData({
        adList: res.result
      });
      //动态滚动关键词
      this.countDownTimer();
    }
  },

  //处理返回的热词
  handleSearchHotKeywordResponseData: function (res) {
    if (res.result.length > 0) {
      this.setData({
        contentStatus: 1,
        keywordList: res.result
      });
    }
  },

  //初始化数据
  initData: function (options) {
    let tmpHistoryList = search_js.historyList();
    this.setData({
      searchHistoryList: tmpHistoryList,
    });
  },

  //搜索内容输入
  bindSearchInput: function (e) {
    let tmpKeyword = e.detail.value;
    this.setData({
      keyword: tmpKeyword
    });
  },

  //取消
  onCancel: function () {
    wx.navigateBack({});
  },

  //大家都在搜索
  onSearchKeyWord: function (e) {
    let index = e.currentTarget.dataset.index;
    let tmpKeyword = this.data.keywordList[index];
    wx.navigateTo({
      url: '../searchResult/searchResult?keyword=' + tmpKeyword
    });
    //记录关键词
    this.addHistoryWithKeyWord(tmpKeyword);

    let tmpInfo = {
      keyWord: tmpKeyword
    }
    //埋点
    ubt_js.myClickButton(common_js.currentPageName(), name_js.searchIndexHot, tmpInfo);
  },

  //删除搜索历史
  onDelteHis: function () {
    //清空数据
    search_js.clearHistory();
    //set data
    this.setData({
      searchHistoryList: []
    });
  },

  //搜索历史
  onHisSearchResult: function (e) {
    let index = e.currentTarget.dataset.index;
    let tmpKeyword = this.data.searchHistoryList[index];
    wx.navigateTo({
      url: '../searchResult/searchResult?keyword=' + tmpKeyword
    });
  },

  //点击键盘搜索按钮
  onSearchDone: function (e) {
    let tmpKeyword = "";
    if (this.data.keyword.length == 0) {
      //关键词是第一给热词
      let item = this.data.adList[0];
      //如果type是宣传语的话，tmpKeyword为空，否则为关键词第一个
      if (item.type == 1) {
        tmpKeyword = item.value;
      } else if (item.type == 2) {
        tmpKeyword = "";
      }
    } else {
      tmpKeyword = this.data.keyword;
    }
    wx.navigateTo({
      url: '../searchResult/searchResult?keyword=' + tmpKeyword
    });

    //添加
    this.addHistoryWithKeyWord(tmpKeyword);


    let tmpInfo = {
      keyWord: tmpKeyword
    }
    //埋点
    ubt_js.myClickButton(common_js.currentPageName(), name_js.searchIndexSearch, tmpInfo);
  },

  //记录搜索关键词
  addHistoryWithKeyWord: function (keyword) {
    //记录历史搜索
    if (keyword.length > 0) {
      search_js.addHistoryWithKeyWord(keyword);
    }
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearTimeout(this.data.timerObj.timer);
    // 业务埋点
    ubt_js.myPageViewOut(common_js.currentPageName());
  },

  // 倒计时计时器**********timerObj
  countDownTimer() { //倒计时函数
    let tmpTimerObj = this.data.timerObj;
    //1s调用一次
    let tmpPlaceholderInfo = this.data.placeholderInfo;
    tmpPlaceholderInfo.placeholder = this.data.adList[tmpPlaceholderInfo.index].value;

    let that = this;
    let timerItem = setTimeout(function () {
      that.countDownTimer();
    }, 3000);
    //清除
    clearTimeout(this.data.timerObj.timer);
    //set data
    tmpPlaceholderInfo.index++;
    if (tmpPlaceholderInfo.index == this.data.adList.length) {
      tmpPlaceholderInfo.index = 0;
    }
    tmpTimerObj.timer = timerItem;
    // 渲染，然后每隔一秒执行一次倒计时函数
    this.setData({
      placeholderInfo: tmpPlaceholderInfo,
      timerObj: tmpTimerObj
    })
  }
  // 倒计时计时器**********
})